<template>
  <div class="pageWrapper">
    <common-header />
    <div class="content">
      <van-tabs
        v-if="tabs.length"
        sticky
        :offset-top="isDD ? 0 : '1.33333rem'"
        v-model="active"
        color="#1B68EB"
        title-inactive-color="#CCCCCC"
        title-active-color="#1F1F1F"
        :line-width="58"
        :line-height="2"
      >
        <van-tab v-for="(item, index) in tabs" :key="index" :title="item.name">
          <appraisal-list :finished="finished" v-if="ready" :ref="'appraisalList' + index" :type="item.type" :appraisalData="appraisalData" />
        </van-tab>
      </van-tabs>
      <common-empty message="暂无审核权限" v-else />
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import appraisalList from "./components/appraisal-list.vue";
import { getEvaluationType, getYearEvaluationType } from "@/api/appraisalProcess";
export default {
  name: "appraisalProcessList",
  components: {
    appraisalList
  },
  data() {
    return {
      active: 0,
      tabs: [],
      appraisalData: {},
      ready: false,
      finished: false,
      flag: false
    };
  },
  computed: {
    ...mapState({
      isDD: state => state.user.is_dd
    })
  },
  beforeRouteEnter(to, from, next) {
    if (from.name == "AppraisalProcess" || !from.name) {
      next(vm => {
        vm.initData();
        vm.active = 0;
        vm.ready = true;
      });
    } else {
      next(vm => {
        if (vm.ready == false) {
          vm.ready = true;
          vm.initData();
        } else {
          vm.flag = true;
        }
      });
    }
  },
  activated() {
    if (this.flag) {
      this.flag = false;
      this.$refs["appraisalList" + this.active][0].onRefresh();
    }
  },
  methods: {
    initData() {
      this.finished = this.$route.query.finished == "0" ? false : true;
      this.appraisalData = JSON.parse(sessionStorage.getItem("appraisalData"));
      let initFn = getYearEvaluationType;
      let form = { deptId: this.appraisalData.deptId, year: this.appraisalData.year };
      if (this.$route.query.type != "year") {
        initFn = getEvaluationType;
        form.quarter = this.appraisalData.quarter;
      }
      this.tabs = [];
      initFn(form).then(res => {
        if (res.data.indexOf("1") != -1) {
          this.tabs.push({
            name: "建议等次",
            type: 1
          });
        }
        if (res.data.indexOf("2") != -1) {
          this.tabs.push({
            name: "评定等次",
            type: 2
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="scss"></style>
